const express = require('express');
const router = express.Router();
const bodyParser = require('body-parser');
const data = require('../data');
let partitionList = data.partitionList;
const noticeSQL = require('../../db/noticeSQL')

router.post('/getnotice', bodyParser.json(), (req, res) => {
    // 检查分区是否为空
    if (req.body["partitionID"] == undefined
        || req.body["partitionID"] == null
        || typeof req.body["partitionID"] != "number") {
        res.json({
            err: 1,
            message: '分区为空'
        })
    } else {
        var partitionID = req.body["partitionID"];
        noticeSQL.getnotice(partitionID, (partition, announcements) => {
            if (partition.length == 0) {
                res.json({
                    err: 1,
                    message: '分区不存在'
                })
            }
            else {
                var tempNoticeList = []
                announcements.forEach(announcement => {
                    tempNoticeList.push({
                        postID: announcement.announcementID,
                        postTitle: announcement.announcementTitle,
                        postTime: announcement.announcementTime,
                        postReplyNum: 0 // to be deleted
                    })
                });
                res.json({
                    err: 200,
                    partitionID,
                    partitionName: partition[0].partitionID,
                    noticeList: tempNoticeList
                })
            }
        })

    }
})

module.exports = router;